package com.gz.im.backend.component.handler;


import cn.hutool.json.JSONUtil;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.gz.im.backend.component.proxy.ImEventProxy;
import com.gz.im.backend.pojo.MessageRequest;
import com.gz.im.backend.pojo.User;
import com.gz.im.backend.pojo.enums.TurnAgentTypeEnum;
import lombok.extern.slf4j.Slf4j;


/**
 * @author GuoZhong
 * @description 客户端nettySocket 处理类
 * @date 2023/3/26 22:50
 */

@Slf4j
public class ImEventHandler {

	private final ImEventProxy proxy;
	public ImEventHandler(ImEventProxy proxy) {
		this.proxy = proxy;
	}
    /**
     * 客户端连接
     */
	@OnConnect
	public void onConnect(SocketIOClient client) {
		log.info("im is connect");
		Long uid = Long.parseLong(client.getHandshakeData().getSingleUrlParam("uid"));
		//初始化连接
		var user=User.builder()
				.id(uid)
				.currentNodeId(1L)
				.turnAgent(TurnAgentTypeEnum.NO.getCode())
				.client(client).build();
		proxy.initOnConnect(uid,user);


	}
	/**
	 * 客户端发生消息
	 */
	@OnEvent("chat")
	public void chat(SocketIOClient client, AckRequest ackRequest, MessageRequest data) {
		log.info("客户端发来的数据:{}",data);
		//客户对话
		proxy.imDialog(data,client);
	}


	/**
	 * 客户端断开
	 */
	@OnDisconnect
	public void onDisconnect(SocketIOClient client) {
		log.info("im is disconnect");
		Long uid = Long.parseLong(client.getHandshakeData().getSingleUrlParam("uid"));
		//初始化断开
		proxy.initOnDisconnect(uid);

	}


}
